function sizeBox(resize) {
	var w = $('html').width();
	var h = $('html').height();
	
	$('#box').css('position', 'absolute');
	if(typeof(resize) == 'undefined') {
		var top = h/2-($('#box').height()/2);
		if (top < 0) {
			top = 0;
		}		
		$('#box').css('top', top);
	}
	
	var left = w/2-($('#box').width()/2);
	if (left < 0) {
		left = 0;
	}	
	
	$('#box').css('left', left);
}

function DBHelp(DBType) {
	if(DBType == "cpanel") {
		LaunchHelp(673);
	}
	else if(DBType == "plesk") {
		LaunchHelp(674);
	}
	else {
		// Other
		LaunchHelp(689);
	}
}

function checkInstallForm() {
	if($('#knowledgebaseName').val() == '') {
		alert(InstallMissingKnowledgebaseName);
		$('#knowledgebaseName').focus();
		return false;
	}
	
	if($('#pathToKB').val() == '' || $('#pathToKB').val().indexOf('http://') != 0 ) {
		alert(InstallMissingPathToKB);
		$('#pathToKB').focus();
		return false;
	}

	if($('#username').val() == '') {
		alert(InstallMissingUsername);
		$('#username').focus();
		return false;
	}
	
	if($('#password').val().length < 3) {
		alert(InstallMissingPassword);
		$('#password').focus();
		$('#password').select();
		return false;
	}

	if($('#password').val() != $('#password1').val()) {
		alert(InstallPasswordMismatch);
		$('#password1').focus();
		$('#password').select();
		return false;
	}

	$('#dbChoice1').click();

	if($('#email').val().indexOf('@') == -1 || $('#email').val().indexOf('.') == -1 || $('#email').val().length <= 3) {
		alert(InstallInvalidEmail);
		$('#email').focus();
		$('#email').select();
		return false;
	}
	
	if($('#email').val() == '') {
		alert(InstallMissingEmail);
		$('#email').focus();
		return false;
	}

	if($('#firstname').val() == '') {
		alert(InstallMissingFirstname);
		$('#firstname').focus();
		return false;
	}

	if($('#lastname').val() == '') {
		alert(InstallMissingLastname);
		$('#lastname').focus();
		return false;
	}

	if ($('#databaseType').val() == 'oci8' && $('#tablePrefix').val().length > 4) {
		alert(InstallTFExceedLength);
		$('#tablePrefix').focus();
		return false;
	}
	
	if($('#mySQLUser').val() == '') {
		alert(InstallMissingDbUser);
		$('#mySQLUser').focus();
		return false;
	}

	if($('#mySQLServer').val() == '') {
		alert(InstallMissingDbServer);
		$('#mySQLServer').focus();
		return false;
	}

	if($('#mySQLDatabase').val() == '') {
		alert(InstallMissingDbDatabase);
		$('#mySQLDatabase').focus();
		return false;
	}

	return true;
}

$(document).ready(function() {
	sizeBox();
	$('.DBDetails').hide();
	$('.DBHelp').hide();

	$('#dbChoice1').click(function() {
		$('.DBDetails').show();
		$('.DBHelp').hide();
	});

	$('#dbChoice2').click(function() {
		$('.DBDetails').hide();
		$('.DBHelp').show();
	});

	$('#frmInstall').submit(function() {
		return checkInstallForm();
	});

	// Are there any permissions problems?
	if(typeof(critical_errors) != 'undefined' && critical_errors == '1') {
		tb_show('', '#TB_inline?height=300&width=450&inlineId=permissionsBox&modal=true', '');
	}
});

$(window).resize(function() {
	sizeBox(true);
});


